var express = require('express');
var request = require('request');
var sha1 = require('sha1');
var app = express();
var noncestr = "asdasdasdasd";
var timestamp = 123;
var page = 'http://www.hzchuangxiangzhe.cn/assets/repair/index.html?code=001asx5c2JWeLQ0bH54c2PEb5c2asx5j&state='
var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx563cd2695406be88&secret=9ac4c54bf56b8891e44077bba628569b"
app.get('/', function(req, res) {
    res.header('Access-Control-Allow-Origin', '*');
    // res.header('Access-Control-Allow-Credentials', true);
    request.get(url, function(error, response, body) {
        var data = JSON.parse(body);
        console.log('access_token: ' + data.access_token);
        request.get('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' + data.access_token + '&type=jsapi', function(error, response, body) {
            var ticket = JSON.parse(body).ticket
            console.log('ticket: ' + ticket);
            var string = 'jsapi_ticket=' + ticket + '&noncestr=' + noncestr + '&timestamp=' + timestamp + '&url=' + page;
            console.log('string: ' + string)
            var signature = sha1(string); //获得签名
            console.log('signature: ' + signature)
            res.json({ signature });
        })
        //console.log(data.access_token)
    })
})
var server = app.listen(8081, function() {
    var host = server.address().address
    var port = server.address().port
    console.log(`listening on ${host}:${port}`);
})